﻿using IDEAMS.core;
using IDEAMS.model;
using IDEAMS.model.setting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace IDEAMS.view.setting
{
    /// <summary>
    /// GlobalSettingDlg.xaml 的交互逻辑
    /// </summary>
    public partial class GlobalSettingDlg : Window
    {
        private GlobalSettingDlgViewModel windowViewModel;
        public GlobalSettingDlg()
        {
            InitializeComponent();

            windowViewModel = new GlobalSettingDlgViewModel();
            this.DataContext = windowViewModel;

            this.btnOK.Click += new RoutedEventHandler(OK_Click);
            this.btnCancel.Click += new RoutedEventHandler(Cancel_Click);
        }

        private void OK_Click(object sender, RoutedEventArgs e)
        {
            if (!CheckInput())
            {
                return;
            }

            GlobalSettingSave();

            this.DialogResult = true;
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private bool CheckInput()
        {
            BindingExpression beFullThreshold = tbFullThreshold.GetBindingExpression(TextBox.TextProperty);
            beFullThreshold.UpdateSource();
            BindingExpression beAdjoinThreshold = tbAdjionThreshold.GetBindingExpression(TextBox.TextProperty);
            beAdjoinThreshold.UpdateSource();

            BindingExpression beContinuousRunTime = tbContinuousRunTime.GetBindingExpression(TextBox.TextProperty);
            beContinuousRunTime.UpdateSource();

            bool hasError = (Validation.GetHasError(tbFullThreshold)
                || Validation.GetHasError(tbAdjionThreshold)
                || Validation.GetHasError(tbContinuousRunTime));

            return !hasError;
        }

        private void GlobalSettingSave()
        {
            GlobalSetting globalSetting = TW_App.globalSetting;

            globalSetting.alarmSoundOn = windowViewModel.globalSetting.alarmSoundOn;
            globalSetting.threshold = new ThresholdSetting(windowViewModel.globalSetting.threshold);
            globalSetting.continuousRunTime = windowViewModel.globalSetting.continuousRunTime;

            globalSetting.Save();
        }
    }

    public class GlobalSettingDlgViewModel : ViewModelBase
    {
        public GlobalSettingDlgViewModel()
        {
            globalSetting =  new GlobalSettingViewModel(TW_App.globalSetting);
        }

        private GlobalSettingViewModel m_globalSetting;
        public GlobalSettingViewModel globalSetting
        {
            get { return m_globalSetting; }
            set
            {
                m_globalSetting = value;
                NotifyPropertyChanged();
            }
        }
    }
}
